package mobi.pruss.multiuser;
import java.util.ArrayList;
import android.content.SharedPreferences;

public class StringListPreference {
	public final static String DEFAULT_PREFIX = "entry";
	
	public static ArrayList<String> getList(SharedPreferences pref) {
		return getList(pref, DEFAULT_PREFIX);
	}
	
	public static ArrayList<String> getList(SharedPreferences pref, String prefix) {
		int i = 0;
		String s;
		ArrayList<String> list = new ArrayList<String>();
		while(null != (s=pref.getString(getKey(prefix, i), null))) {
			list.add(s);
			i++;
		}		
		
		return list;
	}	
	
	private static String getKey(String prefix, int i) {
		return prefix + ".x" + i;
	}
	
	public static void commit(SharedPreferences pref, 
			ArrayList<String> list) {
		commit(pref, DEFAULT_PREFIX, list);
	}
	
	public static void commit(SharedPreferences pref, String prefix,
			ArrayList<String> list) {
		SharedPreferences.Editor ed = pref.edit();
		
		int i = 0;
		
		for (i=0; i<list.size(); i++ ) {
			ed.putString(getKey(prefix,i), list.get(i));
		}
		while (null != pref.getString(getKey(prefix,i), null)) {
			ed.remove(getKey(prefix,i));
			i++;
		}
		
		ed.commit();
	}
}
